package bgw

import (
	"io"
	"net/http"

	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/logic/pay/bgw"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
)

// stanpayng提现回调通知
func BgwPayoutNotifyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.BgwWithdrawCallbackRequest
		// if err := httpx.Parse(r, &req); err != nil {
		//	logc.Error(r.Context(), err)
		//	w.Write([]byte("fail"))
		//	return
		// }

		body, err := io.ReadAll(r.Body)
		if err != nil {
			logc.Error(r.Context(), err)
			w.Write([]byte("fail"))
			return
		}
		defer r.Body.Close()

		l := bgw.NewBgwPayoutNotifyLogic(r.Context(), svcCtx)
		_, err = l.BgwPayoutNotify(r.Header, &req, body)
		if err != nil {
			logc.Error(r.Context(), err)
			w.Write([]byte("fail"))
		} else {
			w.Write([]byte("success"))
		}
	}
}
